package com.vint.mi;

import java.util.Arrays;

/**
 * 1. done
 * 2. hard
 * 过河问题, 得好好分析
 */
public class Mi65 {
    public static String solution(String line){
        String[] src = line.split(",");
        int[] ins = new int[src.length];
        for(int i=0; i<src.length; i++){
            ins[i] = Integer.parseInt(src[i]);
        }
        Arrays.sort(ins);
        return minTime(ins,ins.length-1)+"";
    }

    public static int minTime(int[] src, int length){
        if(length==0){
            return src[0];
        }
        if(length==1){
            return src[1];
        }
        if(length==2){
            return src[0] + src[1] + src[2] ;
        }
        if(src[0] + src[length -1] > 2 * src[1]){
            return minTime(src,length-2) + src[length] + src[0] + 2 * src[1];
        }else{
            return minTime(src,length-1) + src[length] + src[0];
        }
    }

    public static void main(String[] args) {
        String ex1 = "2,3,9,10";
        String ex2 = "2";
        System.out.println(solution(ex1));
        System.out.println(solution(ex2));

    }
}
